<template>
  <div class="app-container">
    <div class="zd3">
      <div class="zd3-title">
        <p>
          <span>分类名称</span>
          <input v-model="username" type="text" placeholder="请输入分类名称" />
        </p>
        <!-- <p>
          <span>部门</span>
          <input v-model="sureusername" type="text" placeholder="请输入部门" />
        </p> -->
        <h3 @click="getList">查询</h3>
        <h4 @click="resetList">重置</h4>
				<h3 @click="addPeople" style="background: #0093FF">新增一级</h3>
      </div>
      <div class="zd3-btm">
        <!-- <div class="zd3-btm-title">
          <p style="flex: 1;">分类名称</p>
          <p style="flex: 1;">创建人</p>
          <p style="flex: 1;">创建时间</p>
          <p style="width: 250px;">操作</p>
        </div> -->
        <!-- <div class="addDate" @click="addPeople">+ 新增一级分类</div> -->
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          :show-header="true"
        >
          <el-table-column type="expand">
            <template slot-scope="props" >
              <div class="children" v-for="(item,index) in props.row.listwarehouseGoods" :key="index">
                <p style="flex: 1;">{{item.name}}</p>
                <p style="flex: 1;">{{item.createName}}</p>
                <p style="flex: 1;">{{item.insertTime}}</p>
                <p style="width:250px">
                  <el-button type="text" size="small" @click="writeChildren(item,index,props.row)">修改</el-button>
                  <el-popconfirm
                    placement="top"
                    hideIcon
                    title="是否要删除此行？"
                    @onConfirm="deleteChildren(item)"
                  >
                    <el-button type="text" size="small" slot="reference">删除</el-button>
                  </el-popconfirm>
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="name" label="分类名称"></el-table-column>
          <el-table-column align="center" prop="createName" label="创建人"></el-table-column>
          <el-table-column align="center" prop="insertTime" label="创建时间"></el-table-column>
          <el-table-column align="center" width="250">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="write(scope.row,scope.$index)">修改</el-button>
              <el-button type="text" size="small" @click="addtwo(scope.row,scope.$index)">添加二级分类</el-button>
              <el-popconfirm
                placement="top"
                hideIcon
                title="是否要删除此行？"
                @onConfirm="deleteRow(scope.row)"
              >
                <el-button type="text" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          hide-on-single-page
          layout="prev, pager, next"
          :total="total"
          @prev-click="goPrev"
          @next-click="goNext"
        />
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false" :modal="true" width="70">
      <div class="change">
        <p>
          <span>分类名称</span>
          <input type="text" v-model="tempData.name" placeholder="请输入分类名称" />
        </p>
        <!-- <p>
          <span>上级分类</span>
          <el-select v-model="value" placeholder="请选择上级分类" class="userselect">
						<el-option v-for="item in tableData" :key="item.id" :label="item.classifyName" :value="item.id"></el-option>
					</el-select>
        </p> -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消保存</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="twoclass" :modal-append-to-body="false" :modal="true" width="70">
      <div class="change">
        <p>
          <span>分类名称</span>
          <input type="text" v-model="twoName" placeholder="请输入分类名称" />
        </p>
        <!-- <p>
          <span>上级分类</span>
          <el-select v-model="value" placeholder="请选择上级分类" class="userselect">
						<el-option v-for="item in tableData" :key="item.id" :label="item.classifyName" :value="item.id"></el-option>
					</el-select>
        </p> -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="twoclass = false">取消保存</el-button>
        <el-button type="primary" @click="saveTwo">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCangku,addCangku,updateCangku,deleteCangku,addOneClassify,addTwoClassify,updateTwoClassify,deleteTwoClassify } from '@/api/cangku'
let oneclassId = null
export default {
  data() {
    return {
      username: "",
      sureusername: "",
      total: 1,
      showHeader: false,
      value: '',
      tableData: [],
      dialogVisible: false,
      twoclass: false,
      twoName: '',  //二级名称
			twoId: '',   //二级id
      tempData: "",
      tempId: "",
      tempIndex: -1,
      twoIndex: -1
    };
  },
  mounted(){
    this.getList()
  },
  methods: {
    changeValue3(value) {},
    getList() {
      getCangku({name: this.username ? this.username : null}).then(res => {
        console.log(res)
        this.tableData = res.data
      })
    },
    // 修改一级分类
    write(item, index) {
      console.log(item,index)
      this.tempData = {...item}
      this.tempIndex = index
      this.dialogVisible = true
    },
    // 修改二级分类
    writeChildren(item,index,oneitem){
      this.twoIndex = index
      this.twoclass = true
      this.twoName = item.name
			this.twoId = item.id
      // oneclassId = oneitem.id
    },
    // 添加二级分类
    addtwo(item){
      console.log(item)
			this.twoName = ''
      this.twoIndex = -1
      this.twoclass = true
      oneclassId = item.id
    },
    // 保存二级分类
    saveTwo(){
      if(this.twoIndex == -1){
        addTwoClassify({name: this.twoName,depositoryId: oneclassId}).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        })
      }else{
        updateTwoClassify({name: this.twoName,id: this.twoId}).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        })
      }
      this.twoclass = false
    },
    // 保存修改一级分类
    save(item) {
      if(this.tempIndex == -1){
        addOneClassify({name: this.tempData.name}).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        })
      }else{
        updateCangku({name: this.tempData.name,id:this.tempData.id}).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        })
      }
      this.dialogVisible = false
    },
    expend(row) {
      let $table = this.$refs.table;
      this.tableData.map(item => {
        if (row.id != item.id) {
          $table.toggleRowExpansion(item, false);
          item.ischecked = false;
        } else {
          item.ischecked = !item.ischecked;
        }
      });
      $table.toggleRowExpansion(row);
    },
    deleteRow(item) {
      console.log("删除");
      console.log(item)
      deleteCangku(item.id).then(res => {
        console.log(res)
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.getList()
      })
    },
		deleteChildren(item){
			console.log(item)
			deleteTwoClassify(item.id).then(res => {
				console.log(res)
				this.$message({
				  message: res.message,
				  type: 'success'
				})
				this.getList()
			})
		},
    goPrev(page) {
      console.log(page);
    },
    goNext(page) {
      console.log(page);
    },
    resetList() {
      (this.username = ""), (this.sureusername = "");
    },
    addPeople(item) {
      this.dialogVisible = true;
      this.tempIndex = -1;
      this.tempData = {
        name: ""
      }
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
}
/deep/.el-button--primary:hover,
.el-button--primary:focus {
  background: #18a05e !important;
  border-color: #18a05e !important;
  color: #fff !important;
}
/deep/.el-button:hover,
.el-button:focus {
  color: #18a05e;
  background-color: transparent;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e;
  border-color: #18a05e;
}
/deep/.el-button--text {
  color: #00c666 !important;
}
/deep/.el-table__expanded-cell[class*=cell]{
  padding: 10px 0px;
}
.children{
  display: flex;
  padding-left: 70px;
  border-bottom: 1px solid #f5f5f5;
  >p{
    text-align: center;
    padding: 5px 0;
    margin: 0;
    line-height: 32px;
    overflow: hidden;
  }
}

.app-container {
  padding: 10px;
}
.status {
  border-radius: 3px;
  font-size: 14px;
  width: 50px;
  color: #fff;
  background: #777777;
  margin: 0 auto;
}
.status-active {
  background: #18a05e;
}
.inps {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: #333;
}
.inps-active {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  // background:#ccc;
  border: none;
  color: #333;
}
.zd3-title {
	height: 80px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
		color: #333;
		margin-right: 30px;

		> span {
			padding-right: 20px;
		}

		> input {
			width: 200px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}

	> h3 {
		width: 64px;
		height: 32px;
		background: #18a05e;
		border-radius: 3px;
		margin: 0 30px 0 0;
		color: #fff;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
	}

	> h4 {
		width: 64px;
		height: 32px;
		border-radius: 3px;
		border: 1px solid rgba(0, 0, 0, 0.1);
		color: #333;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
		margin-right: 30px;
	}
}
/deep/.el-table .cell{
  display: flex;
  justify-content: center;
  align-items: center;
}
/deep/.el-table [class*=el-table__row--level] .el-table__expand-icon{
  display: none;
}
.zd3-btm .btn-prev:after {
  content: "上一页";
}
.zd3-btm .btn-next:before {
  content: "下一页";
}
.zd3-btm .btn-prev,
.zd3-btm .btn-next {
  width: 65px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .el-pagination {
		white-space: nowrap;
		padding: 15px 5px;
		color: #303133;
		font-weight: bold;
	}
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.change {
  display: flex;
  flex-wrap: wrap;
  > p {
    width: 50%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      flex: 1;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
</style>